package com.swinboo.im.endpoint;

import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResultBuilder;
import com.huang.exception.IllegalParamException;
import com.swinboo.im.api.EMMsgApi;
import com.swinboo.im.entity.info.ChatRecordInfo;
import com.swinboo.im.entity.param.GetChatRecordParam;
import com.swinboo.im.entity.param.SendSingleMsgParam;
import com.swinboo.im.entity.repository.ChatRecord;
import com.swinboo.im.service.EMMsgService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Swinboo
 * @date 2022-03-19 20:40
 * @description:
 */
@RestController
public class EMMsgEndpoint implements EMMsgApi {

    @Resource
    private EMMsgService emMsgService;

    @Override
    public HStringResult<Boolean> send(SendSingleMsgParam sendSingleMsgParam) {
        try {
            sendSingleMsgParam.check();
            return emMsgService.send(sendSingleMsgParam);
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    @Override
    public HStringResult<ChatRecordInfo> getRecord(GetChatRecordParam getChatRecordParam) {
        try {
            getChatRecordParam.check();
            return emMsgService.getRecord(getChatRecordParam);
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }
}
